package com.Jshop.goods.Controller;

import com.Jshop.goods.service.GoodHtmlService;
import com.Jshop.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping("item")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodHtmlService goodHtmlService;

    //跳转到商品详情页
    @GetMapping("{id}.html")
    public String toItemPage(Model model, @PathVariable("id") Long id){
        System.out.println(id);
        //加载所需的数据
        Map<String, Object> modelMap = this.goodsService.loadData(id);
        //放入模型
        model.addAllAttributes(modelMap);
        // 页面静态化
        //生成html的代码不能对用户请求产生影响，所以这里我们使用额外的线程进行异步创建。
        this.goodHtmlService.asyncExcute(id);
        //没有静态化的html 则通过templates/下的html进行渲染 返回html元素
        return "item";
    }

}
